package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.impl.dao.VeterinaryDrugsReportDao;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.model.VeterinaryDrugsReport;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by maowenjie on 2017/4/10.
 */
@Component
public class VeterinaryDrugsReportManager {
    private final VeterinaryDrugsReportDao veterinaryDrugsReportDao;

    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public VeterinaryDrugsReportManager(VeterinaryDrugsReportDao veterinaryDrugsReportDao, ReportHandleResumeDao reportHandleResumeDao) {
        this.veterinaryDrugsReportDao = veterinaryDrugsReportDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }


    @Transactional
    public Map<String , Object> createVeterinaryDrugsReport(VeterinaryDrugsReport veterinaryDrugsReport, Integer toStatus){
        Integer fromStatus = veterinaryDrugsReport.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String , Object> map = Maps.newHashMap();

        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "SSY";
        prefix = Utils.createPrefix(prefix,loginUser);//单据编号前缀的开头字母 和 当前用户
        String number = Utils.create(prefix,veterinaryDrugsReportDao.findCurrentMaxNumber(prefix));//要在表中拿到当前最大的Number号
        veterinaryDrugsReport.setNumber(number);
        veterinaryDrugsReport.setDeleteFlag(DocumentsDeleteFlag.NORMAL.value());
        if(toStatus != null && !fromStatus.equals(toStatus)){
            veterinaryDrugsReport.setStatus(toStatus);
            veterinaryDrugsReportDao.create(veterinaryDrugsReport);
            map.put("veterinaryDrugsReportId",veterinaryDrugsReport.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(veterinaryDrugsReport.getId());
            reportHandleResume.setReportNumber(veterinaryDrugsReport.getNumber());
            reportHandleResume.setType(DocumentsType.PRODUCTION_REPORT.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            veterinaryDrugsReportDao.create(veterinaryDrugsReport);
            map.put("veterinaryDrugsReportId",veterinaryDrugsReport.getId());
        }
        return map;
    }

    @Transactional
    public Map<String , Object> updateProductionReport(VeterinaryDrugsReport veterinaryDrugsReport,Integer toStatus){
        Integer fromStatus = veterinaryDrugsReport.getStatus();
        ReportHandleResume reportHandleResume;
        Boolean flagVeterinaryDrugsReport;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            veterinaryDrugsReport.setStatus(toStatus);
            flagVeterinaryDrugsReport = veterinaryDrugsReportDao.update(veterinaryDrugsReport);
            map.put("flagVeterinaryDrugsReport",flagVeterinaryDrugsReport);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(veterinaryDrugsReport.getId());
            reportHandleResume.setReportNumber(veterinaryDrugsReport.getNumber());
            reportHandleResume.setType(DocumentsType.PRODUCTION_REPORT.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            flagVeterinaryDrugsReport = veterinaryDrugsReportDao.update(veterinaryDrugsReport);
            map.put("flagProductionReport",flagVeterinaryDrugsReport);
        }
        return map;
    }
}
